package com.xucia.jsponic.datasource;

import java.io.FileOutputStream;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

public class XmlFileSource extends WritableXmlSource {
    boolean writeOccured = false;
    @Override
	protected synchronized void saveXml() {
        try {
            writeOccured = false;
            System.out.println("Writing XML file: " + getUrl());
            //File tempDest = File.createTempFile("temp","xml");
            FileOutputStream xmlFOS = new FileOutputStream(file, false);
            
            OutputFormat format = new OutputFormat(doc);

            //format.setIndenting(true);
            //format.setIndent(1);
            //format.setLineWidth(0);
            //We comment this out to get pretty formatting, uncomment it to preserve white space
            format.setPreserveSpace(true);
            format.setEncoding("UTF-8");
            XMLSerializer xmlSerializer = new XMLSerializer(xmlFOS,format);  // output to the file using the specified formatting
            xmlSerializer.startPreserving();
            xmlSerializer.serialize(doc);  // serialize the document
            xmlFOS.close();
            /*File tempOldFile = new File("temp.xml");
            file.renameTo(tempOldFile);
            tempDest.renameTo(file);
            tempOldFile.delete();*/
            lastModified = file.lastModified();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }


}
